﻿using IPR_StudyMeetingMrgSys.Enums;
using IPR_StudyMeetingMrgSys.Extension;
using IPR_StudyMeetingMrgSys.Models;
using IPR_StudyMeetingMrgSys.ViewModel;
using IPR_StudyMeetingMrgSys.Common;

using log4net;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Reflection;
using System.Web;
using System.Web.Http;

namespace IPR_StudyMeetingMrgSys.Controllers
{
    public class ModuleController : ApiController
    {
        ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        BLL.Module Bll = new BLL.Module();
        /// <summary>
        /// 添加模块
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/Module/Add")]
        public IHttpActionResult Add(Model.Module model)
        {
            log.Info($"ModID:{model.ModID};ModName:{model.ModName};ModDesc:{model.ModDesc};ModParentID:{model.ModParentID};ModURL:{model.ModUrl};ModLevel:{model.ModLevel}");

            ResultMsg resultMsg = new ResultMsg();
            //验证角色ID RID是否存在  
            if (Bll.Exists(model.ModID))
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = "已经存在该模块ID";
                resultMsg.Data = "";
            }
            else
            {
                Model.Module Mol = new Model.Module();
                Mol.ModID = model.ModID;
                Mol.ModName = model.ModName;
                Mol.ModParentID = model.ModParentID;
                Mol.ModUrl = model.ModUrl;
                Mol.ModDesc = model.ModDesc;
                Mol.ModOrderID = model.ModOrderID;
                Mol.ModLevel = model.ModLevel;
                long flag = Bll.Add(Mol);
                if (flag > 0)
                {
                    resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                    resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                    resultMsg.Data = flag;
                }
                else
                {
                    resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                    resultMsg.Info = "添加失败";
                    resultMsg.Data = flag;
                }
            }

            return Json(resultMsg);
        }
        /// <summary>
       /// 得到模块列表
       /// </summary>
       /// <returns></returns>
        [HttpPost]
        [Route("api/Module/Sel")]
        public IHttpActionResult Sel()
        {
            log.Info($"获取模块列表");
            ResultMsg resultMsg = new ResultMsg();
            DataTable dt= Bll.GetAllList().Tables[0];
            if (dt.Rows.Count > 0)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                resultMsg.Data = dt;
            }
            else
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                resultMsg.Info = "没有获取到模块信息";
                resultMsg.Data = "";
            }
            return Json(resultMsg);
        }
        /// <summary>
        /// 删除模块ModID
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/Module/Del")]
        public IHttpActionResult Del(Model.Module model)
        {
            log.Info($"删除模块");
            log.Info($"ModID:{model.ModID};ModName:{model.ModName};ModDesc:{model.ModDesc};ModParentID:{model.ModParentID};ModURL:{model.ModUrl};ModLevel:{model.ModLevel}");

            ResultMsg resultMsg = new ResultMsg();

            if (Bll.DeleteMod(model.ModID))
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                resultMsg.Data = "";
            }
            else
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                resultMsg.Info = "删除信息失败";
                resultMsg.Data = "";
            }
            return Json(resultMsg);
        }
        /// <summary>
        /// 更新模块
        /// </summary>
        /// <param name="model">mid</param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/Module/Edit")]
        public IHttpActionResult Edit(Model.Module model)
        {
            log.Info($"更新模块列表");
            ResultMsg resultMsg = new ResultMsg();
            Model.Module Mol = new Model.Module();
            Mol.ModID = model.ModID;
            Mol.ModName = model.ModName;
            Mol.ModParentID = model.ModParentID;
            Mol.ModUrl = model.ModUrl;
            Mol.ModDesc = model.ModDesc;
            Mol.ModOrderID = model.ModOrderID;
            Mol.ModLevel = model.ModLevel;
            Mol.AutoID = model.AutoID;
            if (Bll.Update(Mol))
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                resultMsg.Data = "";
            }
            else
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                resultMsg.Info = "更新信息失败";
                resultMsg.Data = "";
            }
            return Json(resultMsg);
        }

        [HttpPost]
        [Route("api/Module/SelTree")]
        public IHttpActionResult GetModuleTreeList()
        {
            log.Info($"获取部门列表");
            ResultMsg resultMsg = new ResultMsg();
            DataTable dt = Bll.GetAllList().Tables[0];
            if (dt.Rows.Count > 0)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                List<VM_Tree> VM_TreeList = new List<VM_Tree>();
                DataRow[] dataRow0 = dt.Select("ModParentID=0");
                Tree.GetTree(dt, dataRow0, ref VM_TreeList, "ModID", "ModParentID", "ModName");
                resultMsg.Data = VM_TreeList;
            }
            else
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                resultMsg.Info = "没有获取到部门信息";
                resultMsg.Data = "";
            }
            return Json(resultMsg);
        }
    }
}
